package com.smedi.ismedi.file.core.application.service.Impl;

import cn.hutool.json.JSONUtil;
import com.smedi.ismedi.cache.starter.util.RedisUtil;
import com.smedi.ismedi.file.core.application.service.LibreOfficeService;
import com.smedi.ismedi.file.core.domain.aggregatemodel.FileInfo;
import com.smedi.ismedi.file.core.domain.service.FileDomainService;
import com.smedi.ismedi.file.core.infrastructure.FileConvert.LibreOfficeUtil;
import com.smedi.ismedi.file.core.infrastructure.config.CommonConfig;
import com.smedi.ismedi.file.core.infrastructure.constant.ConstantField;
import com.smedi.ismedi.file.core.port.adapter.fastdfs.FastDFSClient;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
@AllArgsConstructor
@Slf4j
public class LibreOfficeServiceImpl implements LibreOfficeService {
    private final FileDomainService fileDomainService;

    private final FastDFSClient fastDFSClient;
    private final LibreOfficeUtil libreOfficeUtil;
    private final RedisUtil redisUtil;
    private final CommonConfig commonConfig;

    @Override
    public FileInfo convertFileToPdfWithFile(MultipartFile file) {
        FileInfo fileInfoReturn = libreOfficeUtil.convertToPdf(file);

        //存储文件信息
        return saveFileInfo(fileInfoReturn);
    }

    @Override
    @SneakyThrows
    public FileInfo convertFileToPdfWithFileKey(String fileKey) {
        // 获得文件信息
        FileInfo fileInfo = fileDomainService.getFileInfo(fileKey);
        String inputFileName = fileInfo.getName();
        String shortname = inputFileName.substring(0, inputFileName.lastIndexOf("."));

        // 从fdfs下载文件
        byte[] bytes = fastDFSClient.downloadFile(fileInfo.getStorageGroup(), fileInfo.getPath());

        // 转换成pdf并上传fdfs
        FileInfo fileInfoReturn = libreOfficeUtil.convertToPdf(shortname, fileInfo.getExtName(), bytes);

        //存储文件信息
        return saveFileInfo(fileInfoReturn);
    }

    @Override
    public FileInfo convertFileToPdfWithFileNoSign(MultipartFile file) {
        FileInfo fileInfoReturn = libreOfficeUtil.convertToPdfNoSign(file);

        //存储文件信息
        return saveFileInfo(fileInfoReturn);
    }

    @Override
    @SneakyThrows
    public FileInfo convertFileToPdfWithFileKeyNoSign(String fileKey) {
        // 获得文件信息
        FileInfo fileInfo = fileDomainService.getFileInfo(fileKey);
        String inputFileName = fileInfo.getName();
        String shortname = inputFileName.substring(0, inputFileName.lastIndexOf("."));

        // 从fdfs下载文件
        byte[] bytes = fastDFSClient.downloadFile(fileInfo.getStorageGroup(), fileInfo.getPath());

        // 转换成pdf并上传fdfs
        FileInfo fileInfoReturn = libreOfficeUtil.convertToPdfNoSign(shortname, fileInfo.getExtName(), bytes);

        //存储文件信息
        return saveFileInfo(fileInfoReturn);
    }

    @Override
    public FileInfo convertFileToPdfWithFileNoFree(MultipartFile file) {
        FileInfo fileInfoReturn = libreOfficeUtil.convertToPdfNoFree(file);

        //存储文件信息
        return saveFileInfo(fileInfoReturn);
    }

    @Override
    @SneakyThrows
    public FileInfo convertFileToPdfWithFileKeyNoFree(String fileKey) {
        // 获得文件信息
        FileInfo fileInfo = fileDomainService.getFileInfo(fileKey);
        String inputFileName = fileInfo.getName();
        String shortname = inputFileName.substring(0, inputFileName.lastIndexOf("."));

        // 从fdfs下载文件
        byte[] bytes = fastDFSClient.downloadFile(fileInfo.getStorageGroup(), fileInfo.getPath());

        // 转换成pdf并上传fdfs
        FileInfo fileInfoReturn = libreOfficeUtil.convertToPdfNoFree(shortname, fileInfo.getExtName(), bytes);

        //存储文件信息
        return saveFileInfo(fileInfoReturn);
    }

    /**
     * 存储文件信息
     *
     * @param fileInfo 文件信息
     * @return FileInfo 文件信息返回值
     */
    private FileInfo saveFileInfo(FileInfo fileInfo) {
        FileInfo fileInfoReturn = fileDomainService.fileUpLoad(fileInfo);
        if (Boolean.TRUE.equals(commonConfig.hasUploadFileConfirm)) {
            // 将对象存储到Redis，用于监控，防止有过多作废的文件
            redisUtil.set(ConstantField.profileTempPath + fileInfo.getSignKey(), JSONUtil.toJsonStr(fileInfo), commonConfig.uploadFileConfirmTime);
        }

        return fileInfoReturn;
    }
}

